package com.module.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import base.util.ConstantUnit;
import base.util.Page;
import base.util.StringUtil;

import com.module.product.manager.AttrGroupManager;
import com.module.product.manager.ProductManager;
import com.module.product.vo.AttrGroup;
import com.module.sys.LoginSession;

public class AttrGroupService {
	private AttrGroupManager attrGroupManager = AttrGroupManager.getInstance();	

	public Page list(AttrGroup obj, Integer start, Integer limit)
			throws Exception {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);

		return attrGroupManager.getList(obj, page);
	}
	
		
	public List listAll() throws Exception {
		return attrGroupManager.listAll();
	}

	public List getrootlist(String pid) throws Exception {
		return attrGroupManager.getAttrGroupRootList(pid);
	}

	public List getTreeOne(String id) throws Exception {
		return attrGroupManager.getAttrGroupTreeOne(id);
	}

	public AttrGroup findbyid(String id) {
		try {
			return attrGroupManager.get(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	 
	/**
	 * 根据属性组名和类别查找属性组
	 * @param categoryId
	 * @param name
	 * @return
	 */
	public AttrGroup findAttrGroupByName(String categoryId,String name){
		try {
			 AttrGroup obj=new AttrGroup();
			 obj.setCategoryId(categoryId);
			 obj.setName(name);
			 AttrGroup attrGroup=attrGroupManager.findAttrGroupByName(obj);
			 return attrGroup;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Map save(AttrGroup obj) {
		try {

			if (StringUtils.isBlank(obj.getId()))// 添加
			{
				obj.setCreaterId(LoginSession.getUserid());
			}else// 修改
			{
				obj.setUpdaterId(LoginSession.getUserid());
			}
			boolean isTrue = attrGroupManager.add(obj);
			if ((obj.getRootid() == null || obj.getRootid().equals(""))
					&& obj.getPid().equals("0")) {
				obj.setRootid(obj.getId());
				attrGroupManager.add(obj);
			}
			Map<String, Object> statusMap = new HashMap<String, Object>();
			statusMap.put("state", isTrue);
			if (isTrue == true) {
				statusMap.put("msg", "保存成功");
			} else {
				statusMap.put("msg", "保存失败");
			}
			return statusMap;
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}
	 
	
	public Map remove(List<String> idArray) throws Exception {
		Map<String, Object> statusMap = new HashMap<String, Object>();
		String ids = StringUtil.getNewStrByList(idArray);
		AttrGroup obj = new AttrGroup();
		obj.setId(ids);
		obj.setUpdaterId(LoginSession.getUserid());
		ProductManager productManager=ProductManager.getInstance();
		boolean flag=productManager.getCountByAttrGrop(ids);
		if(!flag){
			boolean isTrue=attrGroupManager.delete(obj);
			statusMap.put("state", isTrue);
			if (isTrue == true) {
				statusMap.put("msg", "保存成功");
			} else {
				statusMap.put("msg", "保存失败");
			}
		}
		return statusMap;
	}
}
